package gold.digger;

import gold.vo.TreeNode;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC437 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int pathSum(TreeNode root, int sum) {
            return pathSumRecur(root, sum, false);
        }

        public int pathSumRecur(TreeNode root, int sum, boolean hasBeenSubtracted) {
            if (null == root) return 0;
            int curNodeCount = root.val == sum ? 1 : 0;
            if (hasBeenSubtracted)
                return curNodeCount + pathSumRecur(root.left, sum - root.val, true) + pathSumRecur(root.right, sum - root.val, true);
            else
                return curNodeCount + pathSumRecur(root.left, sum - root.val, true) + pathSumRecur(root.right, sum - root.val, true)
                        + pathSumRecur(root.left, sum, false) + pathSumRecur(root.right, sum, false);
        }
    }

    public void run() {
        Solution solution = new Solution();
        TreeNode root = TreeNode.createTree("[1,-2,-3,1,3,-2,null,-1]");
        System.out.println(solution.pathSumRecur(root.left, 1, false));
//        System.out.println(solution.pathSum(root.right, 8));
//        System.out.println(solution.pathSum(root.left, 8 - 10));
//        System.out.println(solution.pathSum(root.right, 8 - 10));
        System.out.println(solution.pathSum(root, 1));
    }

    public static void main(String[] args) throws Exception {
        LC437 an = new LC437();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}

